<?php
namespace app\admin\controller;
use think\Request;
use app\common\model\NavModel;
use app\common\controller\Backend;

class Nav extends Backend
{
  public $model;

  public function __construct(){
    $this->model = new NavModel();
    parent::__construct();
  }

	//前台菜单列表
	public function index(Request $request = null)
	{
    //获取全部前台菜单
    $list = $this->model->getList($request->get());

  	return view("index",[
  	'list' => $list,
  	]);
	}

	//前台菜单创建
	public function create(Request $request = null){
    if($request->isPost()){
      $post = $request->post();
      $result = $this->model->allowField(true)->validate(true)->save($post);
      
      if(false === $result){
        return json(['status'=>400,'msg'=>$this->model->getError()]);
      }else{
        return json(['status'=>200,'msg'=>"成功！"]);
      }
    }else{
			return view("create");
    }
	}


  //更新前台菜单
  public function update(Request $request = null){
    if($request->isPost()){
      $post = $request->post();

      $result = $this->model->allowField(true)->validate(true)->save($post,['id'=>$post['id']]);
      if(false === $result){
        return json(['status'=>400,'msg'=>$this->model->getError()]);
      }else{
        return json(['status'=>200,'msg'=>"成功！"]);
      }
    }else{
      $id = $request->param('id/d',0);
      $model = $this->model->findOne($id);

      return view("update",['model'=> $model]);
    }
  }

  /*删除*/
  public function deleteall(Request $request = null){
    if($this->model->deleteall($request->post()['id']) > 0){
      return json(['status'=>200,'msg'=>'成功']);
    }else{
      return json(['status'=>400,'msg'=>'失败']);
    }
  }

  //改变用户状态
  public function changeStatus(Request $request = null){
    $post = $request->post();
    $status = $post['status'];
    $id = $post['id'];

    if($this->model->changeStatus($id, $status) > 0){
      return json(['status'=>200,'msg'=>'成功']);
    }else{
      return json(['status'=>400,'msg'=>'失败']);
    }
  }
}